{% extends "base.html" %}
{%- import "header/package_navigation.html" as navigation -%}

{%- block title -%}
    {% call macros::doc_title(name=metadata.name, version=metadata.version) %}
{%- endblock title -%}

{%- block topbar -%}
  {%- include "rustdoc/topbar.html" -%}
{%- endblock topbar -%}

{%- block header -%}
    {# Set the active tab to the `source` tab #}
    {% call navigation::package_navigation(metadata=metadata, active_tab="source") %}
{%- endblock header -%}

{%- block body_classes -%}
    {%- if file_content.is_some() -%}flex{%- endif -%}
{%- endblock body_classes -%}

{%- block body -%}
    {% set has_file_content = file_content.is_some() %}
    <div class="container package-page-container small-bottom-pad">
        <div class="pure-g">
            <div id="side-menu" class="pure-u-1 {% if has_file_content || is_file_too_large %}pure-u-sm-7-24 pure-u-md-5-24 {% endif %}{% if has_file_content %}source-view{% endif %}">
                <div class="pure-menu package-menu">
                    <ul class="pure-menu-list">
                        {# If we are displaying a file, we also add a button to hide the file sidebar #}
                        {% if has_file_content %}
                            <li class="pure-menu-item toggle-source">
                                <button aria-label="Hide source sidebar" title="Hide source sidebar" aria-expanded="true"><span class="left">{{ crate::icons::IconChevronLeft.render_solid(false, false, "") }}</span><span class="right">{{ crate::icons::IconChevronRight.render_solid(false, false, "") }}</span> <span class="text">Hide files</span></button>
                            </li>
                        {% endif %}
                        {# If this isn't the root folder, show a 'back' button #}
                        {%- if show_parent_link -%}
                            <li class="pure-menu-item">
                                <a href="../" class="pure-menu-link">{{ crate::icons::IconFolderOpen.render_regular(false, false, "") }} <span class="text">..</span></a>
                            </li>
                        {%- endif -%}

                        {%- for file in file_list.files -%}
                            <li class="pure-menu-item">
                                {#
                                    Show a link to the file with a fancy icon. If the file is a directory,
                                    `/` is appended to show the contents of the folder
                                #}
                                <a href="./{{ file.name }}{% if file.mime == "dir" %}/{% endif %}" class="pure-menu-link">
                                    {# Directories #}
                                    {%- if file.mime == "dir" -%}
                                        {{ crate::icons::IconFolderOpen.render_regular(false, false, "") }}

                                    {# Rust files #}
                                    {%- elif file.mime == "text/rust" -%}
                                        {{ crate::icons::IconRust.render_brands(false, false, "") }}

                                    {# Cargo.lock #}
                                    {%- elif file.mime == "text/plain" && file.name == "Cargo.lock" -%}
                                        {{ crate::icons::IconLock.render_solid(false, false, "") }}

                                    {# Markdown files #}
                                    {% elif file.mime == "text/markdown" %}
                                        {{ crate::icons::IconMarkdown.render_brands(false, false, "") }}

                                    {# .gitignore #}
                                    {% elif file.mime == "text/plain" && file.name == ".gitignore" %}
                                        {{ crate::icons::IconGitAlt.render_brands(false, false, "") }}

                                    {#
                                        More ideas
                                            FontAwesome v5:
                                                ".application/x-bzip"
                                               |"application/gzip"
                                               |"application/x-bzip2"
                                               |"application/vnd.rar"
                                               |"application/x-tar"
                                               |"application/zip"
                                               |"application/x-7z-compressed" => https://fontawesome.com/icons/file-archive
                                                "text/javascript" => https://fontawesome.com/icons/js
                                                "application/java-archive" => https://fontawesome.com/icons/java
                                            DevOpticons (https://github.com/file-icons/DevOpicons):
                                                "text/rust" => https://github.com/file-icons/DevOpicons/blob/master/charmap.md#Rust
                                                "text/css" => https://github.com/file-icons/DevOpicons/blob/master/charmap.md#CSS3,%20Full
                                                "text/html" => https://github.com/file-icons/DevOpicons/blob/master/charmap.md#HTML5
                                    #}

                                    {# Text files or files which mime starts with `text` #}
                                    {%- elif file.mime == "text/plain" || file.mime|split_first("/") == Some("text") -%}
                                        {{ crate::icons::IconFileLines.render_regular(false, false, "") }}

                                        {# Binary files and any unrecognized types #}
                                    {% else -%}
                                        {{ crate::icons::IconFile.render_regular(false, false, "") }}
                                    {%- endif -%}

                                    <span class="text">{{ file.name }}</span>
                                </a>
                            </li>
                        {%- endfor -%}
                    </ul>
                </div>
            </div>

            {# If the file exceeds the maximum size, display a warning #}
            {%- if is_file_too_large -%}
                <div id="source-warning" class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
                    <div class="warning">
                        <p>
                            This file is too large to display.
                        </p>
                    </div>
                </div>
            {%- endif -%}

            {# If the file has content, then display it in a codeblock #}
            {%- if let Some(file_content) = file_content -%}
                {% set file_name %}
                {% if let Some(file) = file %}
                    {% set file_name = file.name.as_str() %}
                {% else %}
                    {% set file_name = "" %}
                {% endif %}
                <div id="source-code-container" class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
                    <div data-nosnippet class="source-code"><pre id="line-numbers"><code>
                        {%- for line in 1..=file_content.lines().count() -%}
                            <a href="#{{line}}" id="{{line}}">{{line}}</a>
                        {%~ endfor -%}
                    </code></pre></div>
                    <div id="source-code" class="source-code">
                        {{- file_content|highlight(file_name) -}}
                    </div>
                </div>
            {%- endif -%}
        </div>
    </div>
{%- endblock body -%}

{%- block javascript -%}
    {% if file_content.is_some() %}
        <script nonce="{{ csp_nonce }}" type="text/javascript" src="/-/static/source.js?{{ slug::slugify(crate::BUILD_VERSION) }}"></script>
    {% endif %}
{%- endblock javascript -%}
